home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / conv / Acvt.lha / Acvt 1.07 / sources / switches.pl < prev    next >
Text File  |  1999-09-10  |  5KB  |  324 lines

  1. $infile = "switches.def";
  2. $outfile = "switches.cpp";
  3.  
  4. if ( @ARGV )
  5. {
  6.     $infile = shift @ARGV;
  7. }
  8.  
  9. if ( @ARGV )
  10. {
  11.     $outfile = shift @ARGV;
  12. }
  13.  
  14. ############################################################################
  15. ## Script for generating switches.cpp
  16. ##
  17. ############################################################################
  18.  
  19. open INFILE, $infile or die;
  20. @inf = <INFILE>;
  21. close INFILE;
  22.  
  23. unshift @inf, "=";
  24. unshift @inf, "=bRet = SWFN_HELP( USAGE );";
  25. unshift @inf, "this help";
  26. unshift @inf, "help, ?";
  27.  
  28. push @out, <<LOOP_START;
  29.  
  30. BOOL SWITCHES_Init( int* pargc, char** argv )
  31. {
  32. BOOL bRet = TRUE;
  33.  
  34. m_iArgsCurr = *pargc - 1;
  35. m_iArgsOrig = 1;
  36. m_ppcArgvOrig = argv + 1;
  37. m_ppcArgvCurr = argv + 1;
  38.  
  39. while( m_iArgsCurr )
  40. {
  41. if ( !bRet )
  42.     break;
  43.  
  44. if ( SWITCHES_GetValue() )
  45. {
  46. bRet = TRUE;
  47. *( m_ppcArgvOrig++ ) = * ( m_ppcArgvCurr - 1 );
  48. m_iArgsOrig++;
  49. }
  50. else
  51. {
  52. m_szSwitch++;
  53.  
  54. bRet = TRUE;
  55.  
  56. LOOP_START
  57.  
  58. for(;;)
  59. {
  60.     $line = shift @inf;
  61.  
  62.     last unless defined( $line );
  63.  
  64.     chomp $line;
  65.  
  66.     $line =~ s/\s+$//;
  67.     $line =~ s/^\s+//;
  68.  
  69.     next unless $line;
  70.  
  71.     unshift @inf, $line;
  72.  
  73.     &switch();
  74.  
  75. };
  76.  
  77. push @out, <<LOOP_END;
  78.  
  79. printf( "Invalid switch: %s\\n", *( m_ppcArgvCurr - 1 ) );
  80. return FALSE;
  81. } //endif
  82. } //while
  83.  
  84. *pargc = m_iArgsOrig;
  85. return bRet;
  86. }
  87. LOOP_END
  88.  
  89. if ( $CODE_INCLUDE{ 'SWFN_GETPATH' } )
  90. {
  91.     unshift @out, <<SWFN_GETPATH_CODE;
  92.  
  93. BOOL SWFN_GETPATH( char* szPath )
  94. {
  95. char* szOrigSw = m_szSwitch;
  96.  
  97. if ( !SWITCHES_GetValue() )
  98. {
  99. printf( "Missing value for switch '%s'\\n", szOrigSw );
  100. return FALSE;
  101. }
  102.  
  103. strcpy( szPath, m_szSwitch );
  104. return TRUE;
  105. }
  106.  
  107. SWFN_GETPATH_CODE
  108. }
  109.  
  110. if ( $CODE_INCLUDE{ 'SWFN_NUMH' } )
  111. {
  112.     unshift @out, <<SWFN_NUMH_CODE;
  113.  
  114. BOOL SWFN_NUMH( int* piNum )
  115. {
  116. char* szOrigSw = m_szSwitch;
  117.  
  118. if ( !SWITCHES_GetValue() )
  119. {
  120. printf( "Missing value for switch '%s'\\n", szOrigSw );
  121. return FALSE;
  122. }
  123.  
  124. sscanf( m_szSwitch, "%X", piNum );
  125.  
  126. return TRUE;
  127. }
  128. SWFN_NUMH_CODE
  129. }
  130.  
  131. unshift @out, <<SWFN_HELP_CODE2;
  132. return FALSE;
  133. }
  134.  
  135. SWFN_HELP_CODE2
  136.  
  137. $longest = 0;
  138.  
  139. foreach $line ( @help )
  140. {
  141.     my ( $switch, $desc, $par ) = split( /\|/, $line, 3 );
  142.  
  143.     my $len = length( $switch ) + length( $par );
  144.  
  145.     $longest = $len if ( $len > $longest );
  146. }
  147.  
  148. $longest += 2 + 9;
  149. foreach $line ( @help )
  150. {
  151.     my ( $switch, $desc, $par ) = split( /\|/, $line, 3 );
  152.  
  153.     my $len = length( $switch ) + length( $par );
  154.  
  155.     my $l = "printf( \"-$switch";
  156.  
  157.     if ( $par )
  158.     {
  159.         $l .= " $par";
  160.     }
  161.  
  162.     $l .= ' ' x ( $longest - length( $l ) );
  163.  
  164.     $l .= "  -$desc\\n\" );\n";
  165.  
  166.     push @helpf, $l;
  167.  
  168. }
  169.  
  170. unshift @out, @helpf;
  171.  
  172. unshift @out, <<SWFN_HELP_CODE1;
  173.  
  174. BOOL SWFN_HELP( char* szUsage )
  175. {
  176. if ( szUsage )
  177.     printf( "%s\\n", szUsage );
  178. SWFN_HELP_CODE1
  179.  
  180.  
  181. unshift @out, <<SWITCHES_GLOBALS;
  182.  
  183. int m_iArgsCurr;
  184. int m_iArgsOrig;
  185. char** m_ppcArgvOrig;
  186. char** m_ppcArgvCurr;
  187. char* m_szSwitch;
  188.  
  189. //gets one token from cmdline
  190. BOOL SWITCHES_GetSwitch()
  191. {
  192. if ( !m_iArgsCurr )
  193.     return FALSE;
  194.  
  195. m_szSwitch = *( m_ppcArgvCurr++ );
  196.  
  197. m_iArgsCurr--;
  198.  
  199. return TRUE;
  200. }
  201.  
  202. //gets one token, returns TRUE if value
  203. BOOL SWITCHES_GetValue()
  204. {
  205. if ( !SWITCHES_GetSwitch() )
  206.     return FALSE;
  207.  
  208. switch( *m_szSwitch )
  209. {
  210. case '-':
  211. case '/':
  212.     return FALSE;
  213. default:
  214.     return TRUE;
  215. }
  216. }
  217.  
  218. SWITCHES_GLOBALS
  219.  
  220. open OUTFILE, ">$outfile";
  221.  
  222. print OUTFILE "//THIS IS A GENERATED FILE. DO NOT EDIT!!!\n";
  223. print OUTFILE "//EDIT $infile INSTEAD!\n\n";
  224.  
  225. $indent = 0;
  226.  
  227. $bigline = join( '', @out );
  228. @out = split( /\n/, $bigline );
  229.  
  230. foreach $line ( @out )
  231. {
  232.     $indent-- if ( $line =~ /}/ );
  233.  
  234.     print OUTFILE "\t" x $indent, $line, "\n";
  235.  
  236.     $indent++ if ( $line =~ /{/ );
  237.  
  238. }
  239.  
  240. close OUTFILE;
  241.  
  242. sub switch()
  243. {
  244.     my $switch_desc = "";
  245.  
  246.     my $switches_names = shift @inf;
  247.     my $switches_desc = shift @inf;
  248.  
  249.     chomp $switches_desc;
  250.  
  251.     my @switches = split( /,/, $switches_names );
  252.  
  253.     foreach $switch ( @switches )
  254.     {
  255.         $switch =~ s/\s+$//;
  256.         $switch =~ s/^\s+//;
  257.  
  258.         if ( defined ( $SWITCHES{ $switch } ) )
  259.         {
  260.             die "Duplicate switch $switch";
  261.         }
  262.  
  263.         $SWITCHES{ $switch } = 1;
  264.  
  265.     }
  266.  
  267.     $switches_count = @switches;
  268.  
  269.     $switch_desc = $switches[ 0 ] . "|" . $switches_desc . "|";
  270.  
  271.     push @out, "if (";
  272.  
  273.     my @asc;
  274.  
  275.     foreach $switch (@switches )
  276.     {
  277.         push @asc, "!strcmp( m_szSwitch, \"$switch\" )";
  278.     }
  279.  
  280.     push @out, join( " || ", @asc );
  281.  
  282.     push @out, ")";
  283.     
  284.     push @out, "\n{\n";
  285.  
  286.     my @switch_pars = ();
  287.  
  288.     for(;;)
  289.     {
  290.         my $par_desc = shift @inf;
  291.  
  292.         my ( $par, $code ) = split( /=/, $par_desc, 2 );
  293.  
  294.         $par =~ s/\s+$//;
  295.         $par =~ s/^\s+//;
  296.  
  297.         $code =~ s/\s+$//;
  298.         $code =~ s/^\s+//;
  299.  
  300.         last if ( !$par && !$code );
  301.  
  302.         if ( $code =~ /(SWFN_\w+)/ )
  303.         {
  304.             $CODE_INCLUDE{ $1 } = 1;
  305.         }
  306.  
  307.         if ( $par )
  308.         {
  309.             push @switch_pars, $par;
  310.         }
  311.  
  312.         push @out, $code . "\n";
  313.     }
  314.  
  315.     push @out, "continue;\n";
  316.  
  317.     $switch_desc .= join( ' ', @switch_pars );
  318.  
  319.     push @help, $switch_desc;
  320.  
  321.     push @out, "}\n";
  322.  
  323. }
  324.